package com.longqin.business.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.longqin.business.config.RequiredPermission;
import com.longqin.business.entity.BusExamine;
import com.longqin.business.entity.BusProject;
import com.longqin.business.entity.BusProjectPlan;
import com.longqin.business.entity.BusProjectProblem;
import com.longqin.business.entity.WfNode;
import com.longqin.business.feign.FeignService;
import com.longqin.business.service.IBusExamineService;
import com.longqin.business.service.IBusProjectService;
import com.longqin.business.service.IWfWorkService;
import com.longqin.business.util.ResponseData;
import com.longqin.business.util.ResponseEnum;
import com.longqin.business.util.SessionUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * <p>
 * 项目管理 前端控制器
 * </p>
 *
 * @author longqin
 * @since 2025-01-21
 */
@RestController
@RequestMapping("/project")
public class BusProjectController {

	@Autowired
	IBusProjectService busProjectService;
	
	@Autowired
	IBusExamineService busExamineService;
	
	@Autowired
	IWfWorkService wfWorkService;
	
	@Autowired
	FeignService feignService;
	
	@Value("${project-flow-id}")
	public int FLOW_ID;
	
	@Value("${problem-flow-id}")
	public int PROBLEM_FLOW_ID;
	
	private static final String PROJECT_TABLE_NAME = "bus_project";
	private static final String EXAMINE_TABLE_NAME = "bus_examine";
	private static final String PROBLEM_TABLE_NAME = "bus_project_problem";
	
	/**
	 * @Description 获取单个项目
	 * @Author longqin
	 * @Time: 2025年1月21日
	 */
	@ApiOperation(value = "获取单个项目", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataType = "int") })
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败") })
	@GetMapping("/getById")
	public ResponseData getById(int id) {
		BusProject project = busProjectService.getById(id);
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", project);
	}
	
	/**
	 * @Description 分页获取项目清单
	 * @Author longqin
	 * @Time: 2025年1月21日
	 */
	@ApiOperation(value = "分页获取项目清单", httpMethod = "GET")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "projectName", value = "项目名称", required = true, dataType = "String"),
		@ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "int"),
		@ApiImplicitParam(name = "size", value = "每页数量", required = true, dataType = "int") })
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败"), @ApiResponse(code = 3, message = "参数错误") })
	@GetMapping("/getProjectPage")
	@RequiredPermission("projectList:view")
	public ResponseData getProjectPage(String projectName, Integer page, Integer size) {
		if (null == page || null == size) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		List<BusProject> projectList = busProjectService.getPage(projectName, SessionUtil.getSessionUser().getOrganizationId(), 
				SessionUtil.getSessionUser().getUserId(), (page - 1) * size, size);
		int total = busProjectService.getCount(projectName, SessionUtil.getSessionUser().getOrganizationId(), SessionUtil.getSessionUser().getUserId());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", projectList);
		map.put("total", total);
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", map);
	}
	
	/**
	 * @Description 项目发起
	 * @Author longqin
	 * @Time: 2025年1月21日
	 */
	@ApiOperation(value = "项目发起", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "项目实体项", required = true, dataType = "BusProject") })
	@ApiResponses({ @ApiResponse(code = 1, message = "提交成功，审批流程已启动"), @ApiResponse(code = 0, message = "提交失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/apply")
	@RequiredPermission("projectList:view")
	public ResponseData apply(@RequestBody BusProject entity) throws Exception {
		if (null == entity || null == entity.getProjectName()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		if (entity.getCreator() == null) entity.setCreator(SessionUtil.getSessionUser().getUserId());
		if (entity.getDepartmentId() == null) entity.setDepartmentId(SessionUtil.getSessionUser().getDepartmentId());
		if (entity.getOrganizationId() == null) entity.setOrganizationId(SessionUtil.getSessionUser().getOrganizationId());
		entity.setStatus(1); // 编制状态
		int result = 0;
		if (entity.getId() != null){
			result = busProjectService.update(entity);
		}
		else{
			result = busProjectService.create(entity);
		}
		if (result > 0){
			// 启动审批流程
			result = wfWorkService.dealWork(FLOW_ID, entity.getProcessId(), 1, PROJECT_TABLE_NAME, new ArrayList<String>(), new ArrayList<String>(), 
					SessionUtil.getSessionUser().getUserId(), SessionUtil.getSessionUser().getOrganizationId(), false, entity.getId(), entity.getWorkNodes(), false);
			if (result >= 0){
				// 提交成功，更改项目状态为审核中
				entity.setStatus(2); // 审核状态
				busProjectService.update(entity);
				String nickName = feignService.getNickNameById(result).getData().toString();
				return new ResponseData(ResponseEnum.SUCCESS.getCode(), "提交成功，审批流程已启动", "已发送给审批人：" + nickName);
			}
			else if (result == -2){
				return new ResponseData(ResponseEnum.ERROR.getCode(), "找不到审批人", result);
			}
			else{
				return new ResponseData(ResponseEnum.ERROR.getCode(), "提交失败", result);
			}
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "提交失败", result);
		}
	}
	
	/**
	 * @Description 项目暂存
	 * @Author longqin
	 * @Time: 2025年1月21日
	 */
	@ApiOperation(value = "项目暂存", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "项目实体项", required = true, dataType = "BusProject") })
	@ApiResponses({ @ApiResponse(code = 1, message = "暂存成功"), @ApiResponse(code = 0, message = "暂存失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/save")
	@RequiredPermission("projectList:view")
	public ResponseData save(@RequestBody BusProject entity) throws Exception {
		if (null == entity || null == entity.getProjectName()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		if (entity.getCreator() == null) entity.setCreator(SessionUtil.getSessionUser().getUserId());
		if (entity.getDepartmentId() == null) entity.setDepartmentId(SessionUtil.getSessionUser().getDepartmentId());
		if (entity.getOrganizationId() == null) entity.setOrganizationId(SessionUtil.getSessionUser().getOrganizationId());
		entity.setStatus(1); // 编制状态
		int result = 0;
		if (entity.getId() != null){
			result = busProjectService.update(entity);
		}
		else{
			result = busProjectService.create(entity);
		}
		if (result > 0){
			return new ResponseData(ResponseEnum.SUCCESS.getCode(), "暂存成功", entity.getId());
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "暂存失败", entity.getId());
		}
	}

	/**
	 * @Description 删除项目
	 * @Author longqin
	 * @Time: 2025年1月21日
	 */
	@ApiOperation(value = "删除项目", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "项目ID", required = true, dataType = "int") })
	@ApiResponses({ @ApiResponse(code = 1, message = "删除成功"), @ApiResponse(code = 0, message = "删除失败") })
	@PostMapping("/delete")
	@RequiredPermission("projectList:view")
	public ResponseData delete(int id) throws Exception {
		int result = busProjectService.delete(id);
		if (result >= 0){
			return new ResponseData(ResponseEnum.SUCCESS.getCode(), "删除成功", result);
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "删除失败", result);
		}
	}
	
	/**
	 * @Description 根据流程实例ID获取项目
	 * @Author longqin
	 * @Time: 2025年1月21日
	 */
	@ApiOperation(value = "根据流程实例ID获取项目", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "workId", value = "流程实例ID", required = true, dataType = "int") })
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败") })
	@GetMapping("/getByWorkId")
	public ResponseData getByWorkId(int workId) {
		BusProject project = busProjectService.getByWorkId(workId);
		if (project == null){
			return new ResponseData(ResponseEnum.ERROR.getCode(), "查询失败");
		}
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", project);
	}
	
	/**
	 * @Description 项目审批
	 * @Author longqin
	 * @Time: 2025年1月21日
	 */
	@ApiOperation(value = "项目审批", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "审核实体项", required = true, dataType = "BusExamine") })
	@ApiResponses({ @ApiResponse(code = 1, message = "处理成功"), @ApiResponse(code = 0, message = "处理失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/examine")
	public ResponseData examine(@RequestBody BusExamine entity) throws Exception {
		if (null == entity || null == entity.getExamineStatus() || null == entity.getContent()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		entity.setCreator(SessionUtil.getSessionUser().getUserId());
		entity.setOrganizationId(SessionUtil.getSessionUser().getOrganizationId());
		int result = busExamineService.create(entity);
		if (result > 0){
			// 审批提交
			result = wfWorkService.dealWork(FLOW_ID, entity.getProcessId(), entity.getExamineStatus(), EXAMINE_TABLE_NAME, new ArrayList<String>(), new ArrayList<String>(), 
					SessionUtil.getSessionUser().getUserId(), SessionUtil.getSessionUser().getOrganizationId(), false, entity.getId(), null, false);
			if (result >= 0){
				BusProject project = busProjectService.getByWorkId(entity.getWorkId());
				if (result == 0){ // 结束流程
					// 更改项目状态为发布
					busProjectService.updateStatus(3, project.getId());
					// 更改项目开始时间为审核通过时间
					busProjectService.updateBeginDate(project.getId());
					return new ResponseData(ResponseEnum.SUCCESS.getCode(), "处理成功", "处理成功，流程已结束");
				}
				else{
					if (entity.getExamineStatus() == 0){ //驳回
						// 更改项目状态为编制
						busProjectService.updateStatus(1, project.getId());
					}
					String nickName = feignService.getNickNameById(result).getData().toString();
					return new ResponseData(ResponseEnum.SUCCESS.getCode(), "处理成功", "已发送给下个处理人：" + nickName);
				}
			}
			else if (result == -2){
				return new ResponseData(ResponseEnum.ERROR.getCode(), "找不到审批人", result);
			}
			else{
				return new ResponseData(ResponseEnum.ERROR.getCode(), "处理失败", result);
			}
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "处理失败", result);
		}
	}
	
	/**
	 * @Description 获取流程节点用于节点配置
	 * @Author longqin
	 * @Time: 2025年1月21日
	 */
	@ApiOperation(value = "获取流程节点", httpMethod = "GET")
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败") })
	@GetMapping("/getFlowNodes")
	public ResponseData getFlowNodes() {
		List<WfNode> nodeList = wfWorkService.getFlowNodes(FLOW_ID);
		if (nodeList != null){
			// 剔除开始节点
			List<WfNode> newList = nodeList.stream().filter(s -> s.getGroupSeq() != 1).collect(Collectors.toList());
			return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", newList);
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "查询失败");
		}
	}
	
	/**
	 * @Description 获取项目计划树形结构
	 * @Author longqin
	 * @Time: 2025年4月1日
	 */
	@ApiOperation(value = "获取项目计划树形结构", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "projectId", value = "项目ID", required = true, dataType = "int") })
	@ApiResponses({
	    @ApiResponse(code = 1, message="查询成功"),
	    @ApiResponse(code = 0, message="查询失败")
	})
	@GetMapping("/getPlanTree")
	public ResponseData getPlanTree(int projectId) {
		List<Map<String, Object>> planTree = busProjectService.getPlanTree(projectId);
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", planTree);
	}
	
	/**
	 * @Description 保存项目计划
	 * @Author longqin
	 * @Time: 2025年4月2日
	 */
	@ApiOperation(value = "保存项目计划", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "Integer"),
		@ApiImplicitParam(name = "plans", value = "项目计划清单", required = true, dataType = "List<BusProjectPlan>")})
	@ApiResponses({ @ApiResponse(code = 1, message = "保存成功"), @ApiResponse(code = 0, message = "保存失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/saveProjectPlan")
	@RequiredPermission("projectList:view")
	public ResponseData saveProjectPlan(@RequestBody List<BusProjectPlan> plans, Integer projectId) throws Exception {
		if (null == plans || null == projectId) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		busProjectService.saveProjectPlan(projectId, plans);
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "保存成功");
	}

	/**
	 * @Description 处理项目计划
	 * @Author longqin
	 * @Time: 2025年4月3日
	 */
	@ApiOperation(value = "处理项目计划", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "项目计划数据", required = true, dataType = "BusProjectPlan")})
	@ApiResponses({ @ApiResponse(code = 1, message = "处理成功"), @ApiResponse(code = 0, message = "处理失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/dealProjectPlan")
	@RequiredPermission("projectList:view")
	public ResponseData dealProjectPlan(@RequestBody BusProjectPlan entity) throws Exception {
		if (null == entity || null == entity.getId()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		int result = busProjectService.dealProjectPlan(entity);
		if (result > 0){
			return new ResponseData(ResponseEnum.SUCCESS.getCode(), "处理成功");
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "处理失败");
		}
	}
	
	/**
	 * @Description 分页获取项目问题清单
	 * @Author longqin
	 * @Time: 2025年4月8日
	 */
	@ApiOperation(value = "分页获取项目问题清单", httpMethod = "GET")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "Integer"),
		@ApiImplicitParam(name = "title", value = "问题标题", required = false, dataType = "String"),
		@ApiImplicitParam(name = "beginTime", value = "创建时间-起", required = true, dataType = "String"),
		@ApiImplicitParam(name = "endTime", value = "创建时间-止", required = true, dataType = "String"),
		@ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer"),
		@ApiImplicitParam(name = "size", value = "每页数量", required = true, dataType = "Integer") })
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败"), @ApiResponse(code = 3, message = "参数错误") })
	@GetMapping("/getProjectProblemPage")
	@RequiredPermission("projectList:view")
	public ResponseData getProjectProblemPage(Integer projectId, String title, String beginTime, String endTime, Integer page, Integer size) {
		if (null == projectId || null == page || null == size) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		List<BusProjectProblem> projectList = busProjectService.getProblemPage(projectId, title, beginTime, endTime, (page - 1) * size, size);
		int total = busProjectService.getProblemCount(projectId, title, beginTime, endTime);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", projectList);
		map.put("total", total);
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", map);
	}
	
	/**
	 * @Description 获取问题处理流程节点用于节点配置
	 * @Author longqin
	 * @Time: 2025年4月8日
	 */
	@ApiOperation(value = "获取问题处理流程节点", httpMethod = "GET")
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败") })
	@GetMapping("/getProblemFlowNodes")
	public ResponseData getProblemFlowNodes() {
		List<WfNode> nodeList = wfWorkService.getFlowNodes(PROBLEM_FLOW_ID);
		if (nodeList != null){
			// 剔除开始节点
			List<WfNode> newList = nodeList.stream().filter(s -> s.getGroupSeq() != 1).collect(Collectors.toList());
			return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", newList);
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "查询失败");
		}
	}
	
	/**
	 * @Description 问题发起
	 * @Author longqin
	 * @Time: 2025年4月8日
	 */
	@ApiOperation(value = "问题发起", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "问题实体项", required = true, dataType = "BusProjectProblem") })
	@ApiResponses({ @ApiResponse(code = 1, message = "提交成功，问题处理流程已启动"), @ApiResponse(code = 0, message = "提交失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/problemApply")
	@RequiredPermission("projectList:view")
	public ResponseData problemApply(@RequestBody BusProjectProblem entity) throws Exception {
		if (null == entity || null == entity.getTitle()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		if (entity.getCreator() == null) entity.setCreator(SessionUtil.getSessionUser().getUserId());
		entity.setStatus(1); // 编辑状态
		int result = 0;
		if (entity.getId() != null){
			result = busProjectService.updateProblem(entity);
		}
		else{
			result = busProjectService.createProblem(entity);
		}
		if (result > 0){
			// 启动问题处理流程
			result = wfWorkService.dealWork(PROBLEM_FLOW_ID, entity.getProcessId(), 1, PROBLEM_TABLE_NAME, new ArrayList<String>(), new ArrayList<String>(), 
					SessionUtil.getSessionUser().getUserId(), SessionUtil.getSessionUser().getOrganizationId(), false, entity.getId(), entity.getWorkNodes(), false);
			if (result >= 0){
				// 提交成功，更改问题状态为未解决
				entity.setStatus(2); // 未解决状态
				busProjectService.updateProblem(entity);
				String nickName = feignService.getNickNameById(result).getData().toString();
				return new ResponseData(ResponseEnum.SUCCESS.getCode(), "提交成功，问题处理流程已启动", "已发送给处理人：" + nickName);
			}
			else if (result == -2){
				return new ResponseData(ResponseEnum.ERROR.getCode(), "找不到处理人", result);
			}
			else{
				return new ResponseData(ResponseEnum.ERROR.getCode(), "提交失败", result);
			}
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "提交失败", result);
		}
	}
	
	/**
	 * @Description 问题暂存
	 * @Author longqin
	 * @Time: 2025年4月8日
	 */
	@ApiOperation(value = "问题暂存", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "问题实体项", required = true, dataType = "BusProjectProblem") })
	@ApiResponses({ @ApiResponse(code = 1, message = "暂存成功"), @ApiResponse(code = 0, message = "暂存失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/problemSave")
	@RequiredPermission("projectList:view")
	public ResponseData problemSave(@RequestBody BusProjectProblem entity) throws Exception {
		if (null == entity || null == entity.getTitle()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		if (entity.getCreator() == null) entity.setCreator(SessionUtil.getSessionUser().getUserId());
		entity.setStatus(1); // 编制状态
		int result = 0;
		if (entity.getId() != null){
			result = busProjectService.updateProblem(entity);
		}
		else{
			result = busProjectService.createProblem(entity);
		}
		if (result > 0){
			return new ResponseData(ResponseEnum.SUCCESS.getCode(), "暂存成功", entity.getId());
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "暂存失败", entity.getId());
		}
	}
	
	/**
	 * @Description 获取单个问题
	 * @Author longqin
	 * @Time: 2025年4月8日
	 */
	@ApiOperation(value = "获取单个问题", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "问题ID", required = true, dataType = "int") })
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败") })
	@GetMapping("/getProblemById")
	public ResponseData getProblemById(int id) {
		BusProjectProblem problem = busProjectService.getProblemById(id);
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", problem);
	}
	
	/**
	 * @Description 根据问题处理流程实例ID获取问题
	 * @Author longqin
	 * @Time: 2025年4月8日
	 */
	@ApiOperation(value = "根据问题处理流程实例ID获取问题", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "workId", value = "流程实例ID", required = true, dataType = "int") })
	@ApiResponses({ @ApiResponse(code = 1, message = "查询成功"), @ApiResponse(code = 0, message = "查询失败") })
	@GetMapping("/getProblemByWorkId")
	public ResponseData getProblemByWorkId(int workId) {
		BusProjectProblem problem = busProjectService.getProblemByWorkId(workId);
		return new ResponseData(ResponseEnum.SUCCESS.getCode(), "查询成功", problem);
	}
	
	/**
	 * @Description 问题处理
	 * @Author longqin
	 * @Time: 2025年4月8日
	 */
	@ApiOperation(value = "问题处理", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "问题实体项", required = true, dataType = "BusProjectProblem") })
	@ApiResponses({ @ApiResponse(code = 1, message = "处理成功"), @ApiResponse(code = 0, message = "处理失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/problemDeal")
	public ResponseData problemDeal(@RequestBody BusProjectProblem entity) throws Exception {
		if (null == entity || null == entity.getResolveType() || null == entity.getResolveRemark()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		entity.setStatus(3); // 状态改为已解决
		entity.setResolvePeople(SessionUtil.getSessionUser().getUserId());
		entity.setResolveTime(LocalDateTime.now());
		int result = busProjectService.updateProblem(entity);
		if (result > 0){
			// 流程处理
			result = wfWorkService.dealWork(PROBLEM_FLOW_ID, entity.getProcessId(), 1, PROBLEM_TABLE_NAME, new ArrayList<String>(), new ArrayList<String>(), 
					SessionUtil.getSessionUser().getUserId(), SessionUtil.getSessionUser().getOrganizationId(), false, entity.getId(), null, false);
			if (result >= 0){
				String nickName = feignService.getNickNameById(result).getData().toString();
				return new ResponseData(ResponseEnum.SUCCESS.getCode(), "处理成功", "已发送给下个处理人：" + nickName);
			}
			else if (result == -2){
				return new ResponseData(ResponseEnum.ERROR.getCode(), "找不到下个节点处理人", result);
			}
			else{
				return new ResponseData(ResponseEnum.ERROR.getCode(), "处理失败", result);
			}
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "处理失败", result);
		}
	}
	
	/**
	 * @Description 问题验证
	 * @Author longqin
	 * @Time: 2025年4月10日
	 */
	@ApiOperation(value = "问题验证", httpMethod = "POST")
	@ApiImplicitParams({ @ApiImplicitParam(name = "entity", value = "审核实体项", required = true, dataType = "BusExamine") })
	@ApiResponses({ @ApiResponse(code = 1, message = "处理成功"), @ApiResponse(code = 0, message = "处理失败"),
			@ApiResponse(code = 3, message = "参数错误") })
	@PostMapping("/problemExamine")
	public ResponseData problemExamine(@RequestBody BusExamine entity) throws Exception {
		if (null == entity || null == entity.getExamineStatus() || null == entity.getContent()) {
			return new ResponseData(ResponseEnum.BADPARAM.getCode(), "参数错误");
		}
		entity.setCreator(SessionUtil.getSessionUser().getUserId());
		entity.setOrganizationId(SessionUtil.getSessionUser().getOrganizationId());
		int result = busExamineService.create(entity);
		if (result > 0){
			// 审批提交
			result = wfWorkService.dealWork(PROBLEM_FLOW_ID, entity.getProcessId(), entity.getExamineStatus(), EXAMINE_TABLE_NAME, new ArrayList<String>(), new ArrayList<String>(), 
					SessionUtil.getSessionUser().getUserId(), SessionUtil.getSessionUser().getOrganizationId(), false, entity.getId(), null, true);
			if (result >= 0){
				BusProjectProblem problem = busProjectService.getProblemByWorkId(entity.getWorkId());
				if (result == 0){ // 结束流程
					// 更改问题状态为一关闭
					busProjectService.updateProblemStatus(4, problem.getId());
					return new ResponseData(ResponseEnum.SUCCESS.getCode(), "处理成功", "处理成功，流程已结束");
				}
				else{
					if (entity.getExamineStatus() == 0){ //驳回
						// 更改问题状态为未解决
						busProjectService.updateProblemStatus(2, problem.getId());
					}
					String nickName = feignService.getNickNameById(result).getData().toString();
					return new ResponseData(ResponseEnum.SUCCESS.getCode(), "处理成功", "已发送给下个处理人：" + nickName);
				}
			}
			else if (result == -2){
				return new ResponseData(ResponseEnum.ERROR.getCode(), "找不到审批人", result);
			}
			else{
				return new ResponseData(ResponseEnum.ERROR.getCode(), "处理失败", result);
			}
		}
		else{
			return new ResponseData(ResponseEnum.ERROR.getCode(), "处理失败", result);
		}
	}
}
